CREATE TRIGGER TGS_01 ON SACH AFTER INSERT,UPDATE
AS
BEGIN
	DECLARE @COUNT INT
	SET @COUNT = (SELECT SOLUONG FROM inserted)
	IF(@COUNT < 0)
	BEGIN
		RAISERROR('SO LUONG KHONG DUOC NHO HON 0',16,1)
		ROLLBACK TRAN
		RETURN
	END
END

CREATE TRIGGER TGS_02 ON SACH
INSTEAD OF DELETE
AS
BEGIN
	DECLARE @MASACH VARCHAR(15)
	SET @MASACH = (SELECT MASACH FROM deleted)
	DELETE FROM ChiTietPhieuNhap WHERE MaSach = @MASACH
	DELETE FROM SachNhaXuatBan WHERE MaSach = @MASACH
	DELETE FROM SachTheLoai WHERE MaSach = @MASACH
	DELETE FROM SachTacGia WHERE MaSach = @MASACH
	DELETE FROM Sach WHERE MaSach = @MASACH
	RAISERROR('XOA SACH THANH CONG',16,1)
END